package com.itany.corejava.day11;

import java.util.Scanner;

public class TestException {


    public static void main(String[] args) {
        // f1();

        // f2();

        // System.out.println(f3());



        Scanner sc=new Scanner(System.in);

        System.out.println("用户登录");
        System.out.println("用户名：");
        String username=sc.nextLine();
        System.out.println("密码：");
        String password=sc.nextLine();

        try {
            login(username,password);
            System.out.println("登录成功");
        } catch (UserNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (UserDisabledException e) {
            System.out.println(e.getMessage());
        }

    }


    public static void f1() throws Exception{
        // int i=1/0;

        // Exception e=new Exception("错误消息");
        // throw e;
        // System.out.println("代码");



    }

    public static void f2(){

        try {
            Exception e = new Exception("错误消息");
            throw e;

            // int i=1/0;

        }catch (ArithmeticException e){
            System.out.println("0不能作为除数");
        }catch (Exception e){
            System.out.println("异常处理代码");
        }
        System.out.println("代码");
    }

    public static int f3(){

        try{
            System.out.println("try");
            int i=1/0;
            return 1;
        }catch (Exception e){
            System.out.println("catch");
            System.out.println(e.getMessage());
            e.printStackTrace();
            return 2;
        }finally{
            //必须执行
            System.out.println("finally");
            return 3;
        }


    }


    public static void login(String username,String password)throws UserNotFoundException,UserDisabledException{

        if("abc".equals(username)&&"123".equals(password)){
            return;
        }

        if("aaa".equals(username)&&"111".equals(password)){
            throw new UserDisabledException("用户已被禁用");
        }

        throw new UserNotFoundException("用户名或密码错误");



    }




}
